package com.example.task.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.example.task.MainActivity;
import com.example.task.R;
import com.example.task.bean.Comment;
import com.example.task.bean.Lecture;
import com.example.task.bean.SocketUser;
import com.example.task.bean.Teacher;
import com.example.task.bean.User;
import com.example.task.bean.User_Signup;
import com.example.task.delegate.SimpleDelegateAdapter;
import com.example.task.http.Threads.GetEvaluateThread;
import com.example.task.http.Threads.ListStudentInfoThread;
import com.example.task.http.Threads.SignUpBackNumByActIdThread;
import com.example.task.socket.SocketActivity;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;

public class Teacher_EvaluateActivity extends AppCompatActivity {

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_act);
        SimpleDelegateAdapter<User_Signup> mNewsAdapter;
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(Teacher_EvaluateActivity.this);
        recyclerView.setLayoutManager(layoutManager);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
//        User user = (User) bundle.getSerializable("User");
        Lecture lecture = (Lecture) bundle.getSerializable("Lecture");
        Teacher user = (Teacher) bundle.getSerializable("Teacher");

        System.out.println("???" + user);
        List<Lecture> lectureList = new ArrayList<>();
        lectureList.add(lecture);
        TextView textView1 = findViewById(R.id.TextView1);
        textView1.setText(lecture.getActivityName());
        TextView textView2 = findViewById(R.id.TextView2);
        textView2.setText(lecture.getSignUpTime() + " ~ " + lecture.getDeadlineForSign());
        TextView textView3 = findViewById(R.id.Time);
        textView3.setText("时间：" + lecture.getInitDate() + " " + lecture.getAtivityTime() + "-" + lecture.getDeadlineForAtivity());
        TextView textView4 = findViewById(R.id.Place);
        textView4.setText("地点：" + lecture.getAtivityPlace());
        TextView textView5 = findViewById(R.id.Principal);
        textView5.setText("负责人工号：" + lecture.getInChargeID());
        TextView textView6 = findViewById(R.id.Phone);
        textView6.setText("联系方式：" + lecture.getInChargeNB());
        TextView textView7 = findViewById(R.id.Type);
        textView7.setText(lecture.getType());
        TextView textView8 = findViewById(R.id.Them);
        textView8.setText( lecture.getTheme());
        TextView textView9 = findViewById(R.id.summery);
        textView9.setText(lecture.getSummery());
        TextView textView10 = findViewById(R.id.signup);
        FormBody.Builder builder1 = new FormBody.Builder();
        builder1.add("actID", lecture.getActivityId());
        SignUpBackNumByActIdThread thread1 = new SignUpBackNumByActIdThread(builder1);
        thread1.start();
        try {
            thread1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("??"+thread1.value);
        JsonObject obj1 = new Gson().fromJson(thread1.value, JsonObject.class);
        textView10.setText("签到名单");

        FormBody.Builder builder = new FormBody.Builder();
        System.out.println("??????????"+lecture.getInChargeID());
        builder.add("username",lecture.getInChargeID());
        ListStudentInfoThread thread = new ListStudentInfoThread(builder);
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(thread.value);
        JsonArray obj = new Gson().fromJson(thread.value, JsonArray.class);
        List<User_Signup> userlist = new ArrayList<>();
        for(JsonElement jsonElement:obj){
            String activityID = jsonElement.getAsJsonObject().get("activityID").toString().replace("\"","");//活动id
            if(activityID.equals(lecture.getActivityId())){
                String iduser = jsonElement.getAsJsonObject().get("iduser").toString().replace("\"","");//学生id
                String username = jsonElement.getAsJsonObject().get("username").toString().replace("\"","");//学生id
                String password = jsonElement.getAsJsonObject().get("password").toString().replace("\"","");//学生密码
                String name = jsonElement.getAsJsonObject().get("name").toString().replace("\"","");//学生名字
                String email = jsonElement.getAsJsonObject().get("email").toString().replace("\"","");//学生邮箱
                String telephone = jsonElement.getAsJsonObject().get("telephone").toString().replace("\"","");//学生联系方式
                String birthday = jsonElement.getAsJsonObject().get("birthday").toString().replace("\"","");//注册时间
                String sex = jsonElement.getAsJsonObject().get("sex").toString().replace("\"","");//性别
                String initdate = jsonElement.getAsJsonObject().get("initdate").toString().replace("\"","");
                String classRoom = jsonElement.getAsJsonObject().get("classRoom").toString().replace("\"","");//学生班别
                String faculty = jsonElement.getAsJsonObject().get("faculty").toString().replace("\"","");//学生院系
                String imgpath = jsonElement.getAsJsonObject().get("imgpath").toString().replace("\"","");//头像
                String signupID = jsonElement.getAsJsonObject().get("signupID").toString().replace("\"","");//报名id
                String activityName = jsonElement.getAsJsonObject().get("activityName").toString().replace("\"","");//活动名
                String time = jsonElement.getAsJsonObject().get("time").toString().replace("\"","");//报名时间
                String qrStates = jsonElement.getAsJsonObject().get("qrStates").toString().replace("\"","");//签到状态
                User_Signup user1 = new User_Signup(Integer.parseInt(iduser),username, password, name, email, telephone,
                        birthday, sex, initdate, classRoom, faculty, imgpath, signupID, activityID, activityName, time, qrStates);
                userlist.add(user1);
            }

        }
        mNewsAdapter = new SimpleDelegateAdapter<User_Signup>(R.layout.activity_teacher_act_item, new LinearLayoutHelper(),userlist) {

            @Override
            protected void bindData(@NonNull RecyclerViewHolder holder, int position, User_Signup user_signup) {
                if (user_signup != null) {

                    holder.text(R.id.name, user_signup.getUsername());
                    holder.text(R.id.faculty, user_signup.getFaculty()+" "+user_signup.getClassRoom());
                    holder.click(R.id.comment, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Bundle bundle1 = new Bundle();
                            Intent intent = new Intent(Teacher_EvaluateActivity.this, Student_MessageActivity.class);
                            bundle1.putSerializable("User_Signup", user_signup);
                            intent.putExtras(bundle1);
                            startActivity(intent);
                        }
                    });
                }
            }
        };
        recyclerView.setAdapter(mNewsAdapter);
        TextView talk = findViewById(R.id.talk);
        talk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(Teacher_EvaluateActivity.this, SocketActivity.class);
                SocketUser socketUser = new SocketUser();
                socketUser.setActivityId(lecture.getActivityId());
                socketUser.setRole("teacher");
                socketUser.setRoleName(user.getFullName());
                Bundle bundle1 = new Bundle();
                bundle1.putSerializable("SocketUser",socketUser);
                intent1.putExtras(bundle1);
                startActivity(intent1);
            }
        });
//        mNewsAdapter = new SimpleDelegateAdapter<User_Signup>(R.layout.activity_list_studeny_info_item, new LinearLayoutHelper(),userlist) {
//
//            @Override
//            protected void bindData(@NonNull RecyclerViewHolder holder, int position, User_Signup user_signup) {
//                if (user_signup != null) {;
//                    holder.image(R.id.image1, imageViews);
//                    holder.text(R.id.text1, user_signup.getUsername());
//                    holder.text(R.id.text2, user_signup.getClassRoom());
//                    holder.text(R.id.text3,user_signup.getFaculty());
//                    holder.text(R.id.text4,user_signup.getTime());
//                    holder.click(R.id.message, new View.OnClickListener() {
//                        @SingleClick
//                        @Override
//                        public void onClick(View v) {
//                            Bundle bundle1 = new Bundle();
//                            Intent intent = new Intent(ListStudentInfoActivity.this, Student_MessageActivity.class);
//                            bundle1.putSerializable("User_Signup", user_signup);
//                            intent.putExtras(bundle1);
//                            startActivity(intent);
//                        }
//                    });
//                }
//            }
//        };
//        recyclerView.setAdapter(mNewsAdapter);
    }
}